/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project.jpdftools.ui.actionlisteners;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.logging.Level;
import javax.swing.*;
import javax.swing.border.Border;
import project.jpdftools.ui.logic.JPDFTable;
import project.jpdftools.ui.MainMenu;
import project.jpdftools.managers.LocalizationManager;
import project.jpdftools.managers.LoggerManager;

/**
 *
 * @author Mido
 */
public class AddTablePopUpMenuButtonAction implements ActionListener, MouseListener {

    private JButton[][] table_dimension;
    private JPopupMenu menu;
    private JLabel table_size_label;
    private int row;
    private int column;

    public AddTablePopUpMenuButtonAction(JButton[][] table_dimension, JPopupMenu menu, JLabel table_size_label, int row, int column) {
        super();
        this.table_dimension = table_dimension;
        this.table_size_label = table_size_label;
        this.menu = menu;
        this.row = row;
        this.column = column;
    }

    public void actionPerformed(ActionEvent evt) {
        // hide the popup menu
        menu.setVisible(false);
        // add the table with the selected rows and columns
        JPDFTable table = new JPDFTable(row + 1, column + 1);
        MainMenu.getInstance().addObject(table);
        // restore defaults
        restoreDefaults();
        LoggerManager.getLogger().log(Level.INFO, "New table added. Rows = {0}, Columns = {1}. Component ID = {2}",
                new Object[]{row + 1, column + 1, table.getId()});
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
        // create highlight & default border
        Border highlight_border = BorderFactory.createLineBorder(Color.orange);
        Border default_border = UIManager.getBorder(JButton.class);

        // show table size in the label
        table_size_label.setText((row + 1) + " X " + (column + 1));
        // highlight selected buttons
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (i <= row && j <= column) {
                    table_dimension[i][j].setBorder(highlight_border);
                } else {
                    table_dimension[i][j].setBorder(default_border);
                }
            }
        }
    }

    public void mouseExited(MouseEvent e) {
    }

    /**
     * Restore popup menu defaults after selecting any button or menu item
     */
    private void restoreDefaults() {
        // restore label text
        table_size_label.setText(LocalizationManager.getCurrent_resource().getString("ADD_TABLE_PANEL_TITLE"));

        Border default_border = UIManager.getBorder(JButton.class);

        // restore button defaluts
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (i <= row && j <= column) {
                    table_dimension[i][j].setBorder(default_border);
                }
            }
        }
    }
}
